package org.fage.semples.service;

import java.util.ArrayList;
import java.util.List;

import org.fage.semples.dao.ExamScoreHiberanteDAO;
import org.fage.semples.dao.ExamUserSubjectHiberanteDAO;
import org.fage.semples.model.ExamScore;
import org.fage.semples.model.ExamUserSubject;
import org.fage.semples.model.ExamUserSubjectScoreVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ExamUserSubjectHiberanteServiceImpI implements ExamUserSubjectHiberanteService {

	@Autowired
	private ExamUserSubjectHiberanteDAO examUserSubjectHiberanteDAO;
	
	@Autowired
	private ExamScoreHiberanteDAO examScoreHiberanteDAO;
	
	@Override
	public List<ExamUserSubject> findAllExamUserSubjects() {
		return examUserSubjectHiberanteDAO.findAllExamUserSubjects();
	}

	@Override
	public List<ExamUserSubjectScoreVO> getByUserIdExamUserSubjects(String userId) {
		List<ExamUserSubject> examUserSubjectList = examUserSubjectHiberanteDAO.getByUserIdExamUserSubjects(userId);
		List<ExamUserSubjectScoreVO> examUserSubjectScoreVOList =new ArrayList<ExamUserSubjectScoreVO>();
		List<ExamScore> examScoreList =  examScoreHiberanteDAO.getScoreByUserId(userId);
		ExamUserSubjectScoreVO examUserSubjectScoreVO = null;
		
		for(ExamUserSubject examUserSubject:examUserSubjectList){
			examUserSubjectScoreVO = new ExamUserSubjectScoreVO();
			BeanUtils.copyProperties(examUserSubject.getExamUser(), examUserSubjectScoreVO);
			BeanUtils.copyProperties(examUserSubject.getExamSubject(), examUserSubjectScoreVO);
			examUserSubjectScoreVOList.add(examUserSubjectScoreVO);
		}
		
		for(ExamScore examScore:examScoreList){
			for(ExamUserSubjectScoreVO examUserSubjectScore:examUserSubjectScoreVOList){
				if(examUserSubjectScore.getSubId().equals(""+examScore.getSubjectId())){
					examUserSubjectScore.setScore(examScore.getScore());
					examUserSubjectScore.setScoreId(examScore.getId());
					examUserSubjectScore.setExamTime(examScore.getExamTime());
				}
			}
		}
		
		return examUserSubjectScoreVOList;
	}
	
}
